<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
jimport('joomla.application.component.modellist');
/**
 * CategoryList Model
 */
class ShopModelProducts extends JModelList
{
	/**
	 * Returns a reference to the a Table object, always creating it.
	 *
	 * @param	type	The table type to instantiate
	 * @param	string	A prefix for the table class name. Optional.
	 * @param	array	Configuration array for model. Optional.
	 * @return	JTable	A database object
	 * @since	1.6
	 */
	protected $item = null;
	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
		$app = JFactory::getApplication();
		$session = JFactory::getSession();

		// Adjust the context to support modal layouts.
		if ($layout = JRequest::getVar('layout')) {
			$this->context .= '.'.$layout;
		}

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);

	$category = $this->getUserStateFromRequest($this->context.'.filter.category', 'filter_category', '');
		$this->setState('filter.category', $category);
		
		// List state information.
		parent::populateState('id', 'desc');
	} 
	public function getTable($type = 'Product', $prefix = 'ShopTable', $config = array()) 
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return	string	An SQL query
	 */
	protected function getListQuery()
	{
		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);
 		$query->select("p.*");
		$query->from("#__shop_product as p");
		
		$search = $this->getState('filter.search');
		if(!empty($search)){
			if(is_numeric($search)){
				$query->where('p.id = '.(int)$search);
			}else{
			$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
			$query->where('p.vtitle LIKE '.$search);
			}
		}
		$category = $this->getState('filter.category');
		if(is_numeric($category)){
			$query->where('p.catid = '.(int)$category);
		}
		$query->order("p.id desc");
		return $query;
	}
	public function getForm($data = array(), $loadData = true) 
	{
		// Get the form.
		return null;
	}

}